package com.ejlchina.tool.fragment.offline

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import com.adolf.androiddemo.databinding.FragmentInputSignalOfflineBinding
import com.ejlchina.tool.adapter.DeviceAdapterRadioOffline
import com.ejlchina.tool.base.BaseBindingFragment2
import com.ejlchina.tool.bean.BleDeviceBean
import com.ejlchina.tool.bean.NpnInputBean
import com.ejlchina.tool.enums.NpnEnum
import com.ejlchina.tool.singleton.GlobalConnectObject
import com.ejlchina.tool.util.COMMAND_NPN_SIGNAL_INPUT_SET_DOWNWARD
import com.ejlchina.tool.util.COMMAND_NPN_SIGNAL_INPUT_SET_UPWARD
import com.ejlchina.tool.util.COMMAND_SIGNAL_INPUT_GET_OFFLINE_DOWNWARD
import com.ejlchina.tool.util.COMMAND_SIGNAL_INPUT_GET_OFFLINE_UPWARD
import com.ejlchina.tool.util.bytearray.intTo32HexStringLE
import com.ejlchina.tool.util.bytearray.readByteArrayLE
import com.ejlchina.tool.util.getBitValueFrom32Bits
import com.ejlchina.tool.util.setBitValue
import com.ejlchina.tool.util.showToast
import com.ejlchina.tool.view.GridItemDecoration
import com.huyuhui.fastble.data.BleDevice

class InputSignalOfflineFragment: BaseBindingFragment2<FragmentInputSignalOfflineBinding>() {
    
    private val inputSignalBeanLeft = NpnInputBean()
    private val inputSignalBeanRight = NpnInputBean()
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initViews()
        initData()
        
    }
    
    private fun initViews(){
        setHeadTitle("输入信号")
        setRightIconVisibility(View.GONE)
        initListeners()
    }

    private fun initData(){
        initDevices()
    }

    private fun initDevices(){
        deviceAdapterOffline = DeviceAdapterRadioOffline(GlobalConnectObject.getCurrentConnectDeviceList()){
            getInputSignalData(it)
        }
        binding.rvDevices.apply {
            adapter = deviceAdapterOffline
            layoutManager = GridLayoutManager(requireContext(),3)
            if (itemDecorationCount == 0){
                addItemDecoration(GridItemDecoration(3,11,11))
            }
        }
    }

    private fun getResultData(): String{
        var npnValue = 0
        if (!inputSignalBeanLeft.full){
            npnValue = setBitValue(npnValue,0)
        }
        if (!inputSignalBeanRight.full){
            npnValue = setBitValue(npnValue,1)
        }
        if (!inputSignalBeanLeft.body){
            npnValue = setBitValue(npnValue,2)
        }
        if (!inputSignalBeanRight.body){
            npnValue = setBitValue(npnValue,3)
        }
        if (!inputSignalBeanLeft.button){
            npnValue = setBitValue(npnValue,4)
        }
        if (!inputSignalBeanRight.button){
            npnValue = setBitValue(npnValue,5)
        }
        return intTo32HexStringLE(npnValue)
    }

    private fun setSignalInputData(bleDevice: BleDeviceBean){
        setCommandData(bleDevice, COMMAND_NPN_SIGNAL_INPUT_SET_DOWNWARD,getResultData())
    }

    private fun initListeners(){

        binding.btnConfirm.setOnClickListener {
            setSignalInputData(deviceAdapterOffline.getSelectDevice())
        }
        binding.msFullLeft.setOnCheckedChangeListener { _, isChecked ->
            inputSignalBeanLeft.full = isChecked
        }
        binding.msFullRight.setOnCheckedChangeListener { _, isChecked ->
            inputSignalBeanRight.full = isChecked
        }
        binding.msHumanLeft.setOnCheckedChangeListener { _, isChecked ->
            inputSignalBeanLeft.body = isChecked
        }
        binding.msHumanRight.setOnCheckedChangeListener { _, isChecked ->
            inputSignalBeanRight.body = isChecked
        }
        binding.msButtonLeft.setOnCheckedChangeListener { _, isChecked ->
            inputSignalBeanLeft.button = isChecked
        }
        binding.msButtonRight.setOnCheckedChangeListener { _, isChecked ->
            inputSignalBeanRight.button = isChecked
        }
    }

    override fun whenReceiveCommand(bleDevice: BleDeviceBean, command: String, commandByteArray: ByteArray) {
        super.whenReceiveCommand(bleDevice,command, commandByteArray)
        when (command){
            COMMAND_SIGNAL_INPUT_GET_OFFLINE_UPWARD -> {
                setInputSingleBeanData(commandByteArray.readByteArrayLE(0,commandByteArray.size))
                setUIData()
            }
            COMMAND_NPN_SIGNAL_INPUT_SET_UPWARD -> {
                val resultInt = commandByteArray[4].toInt()
                val toastString = if (resultInt == 1) "设置成功" else "设置失败"
                showToast(toastString)
            }
        }
        dismissLoading()
    }

    private fun setInputSingleBeanData(dataByteArray: ByteArray){
        inputSignalBeanLeft.full = getBitValueFrom32Bits(dataByteArray,0) == NpnEnum.OPEN.value
        inputSignalBeanRight.full = getBitValueFrom32Bits(dataByteArray,1) == NpnEnum.OPEN.value
        inputSignalBeanLeft.body = getBitValueFrom32Bits(dataByteArray,2) == NpnEnum.OPEN.value
        inputSignalBeanRight.body = getBitValueFrom32Bits(dataByteArray,3) == NpnEnum.OPEN.value
        inputSignalBeanLeft.button = getBitValueFrom32Bits(dataByteArray,4) == NpnEnum.OPEN.value
        inputSignalBeanRight.button = getBitValueFrom32Bits(dataByteArray,5) == NpnEnum.OPEN.value
    }

    private fun setUIData(){
        binding.msFullLeft.isChecked = inputSignalBeanLeft.full
        binding.msFullRight.isChecked = inputSignalBeanRight.full
        binding.msHumanLeft.isChecked = inputSignalBeanLeft.body
        binding.msHumanRight.isChecked = inputSignalBeanRight.body
        binding.msButtonLeft.isChecked = inputSignalBeanLeft.button
        binding.msButtonRight.isChecked = inputSignalBeanRight.button
    }

    private fun getInputSignalData(bleDevice: BleDeviceBean){
        getCommandData(bleDevice, COMMAND_SIGNAL_INPUT_GET_OFFLINE_DOWNWARD)
    }
    override fun getViewBinding(inflater: LayoutInflater, container: ViewGroup): FragmentInputSignalOfflineBinding {
        return FragmentInputSignalOfflineBinding.inflate(inflater,container,true)
    }
}